package com.baicaoyuan.web.repository;


import com.baicaoyuan.web.model.JzzUserDO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;

/**
 * 功能描述：
 *
 * @author 罗富祥
 * 创建时间： 2018/1/4
 * 版权：
 */
@Repository
public interface JzzUserRepository extends JpaRepository<JzzUserDO, Long> {

    JzzUserDO findByUserId(Long userId);

    @Transactional
    @Modifying
    @Query(value = "update t_jzz_user set jzz_number_dig = jzz_number_dig + ?2, jzz_number = jzz_number_dig - jzz_number_cash where user_id_fk = ?1", nativeQuery = true)
    Integer updateJzzNumberDig(Long userId, BigDecimal jzzNumberDig);

    @Transactional
    @Modifying
    @Query(value = "update t_jzz_user set jzz_number_cash = jzz_number_cash + ?2, jzz_number = jzz_number_dig - jzz_number_cash where user_id_fk = ?1 ", nativeQuery = true)
    Integer updateJzzNumberCash(Long userId, BigDecimal jzzNumberCash);

    Integer countAllByUserId(Long userId);

    Integer countAllBy();

    @Query(value = "select ju.* from t_jzz_user ju left JOIN t_user u on ju.user_id_fk = u.id  where u.telephone  = ?1 ORDER BY ju.create_time DESC Limit ?2,?3", nativeQuery = true)
    List<JzzUserDO> queryByUserPhone(String phone , Integer pageNo, Integer pageSize);

    @Query(value = "select * from t_jzz_user ORDER BY create_time DESC Limit ?1,?2", nativeQuery = true)
    List<JzzUserDO> queryUsers( Integer pageNo, Integer pageSize);
}
